package sunwin.yoms.service.third;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sunwin.yoms.comm.YomsException;
import sunwin.yoms.dao.ThirdLandDao;
import sunwin.yoms.domain.third.ThirdLandDomain;

import java.util.List;

/**
 * Created by watson on 2015/9/10.
 */
@Service
public class ThirdLandService {
    @Autowired
    private ThirdLandDao thirdLandDao;

    /**
     * 查询所有第三方登陆
     */
    public List<ThirdLandDomain> findThirdLand(ThirdLandDomain thirdLandDomain) throws YomsException {
        List<ThirdLandDomain> domains = thirdLandDao.getDomainCollection(thirdLandDomain);
        if (domains.size() == 0) {
            throw new YomsException("没有找到数据", 1);
        }
        return domains;
    }

    /**
     * 删除第三方登陆
     */
    public void delThirdLand(int id) throws YomsException {
        ThirdLandDomain thirdLandDomain1 = thirdLandDao.getDomainById(id);
        if (thirdLandDomain1 == null) {
            throw new YomsException("非法请求");
        } else {
            if (thirdLandDao.deleteDomain(thirdLandDomain1) != 1) {
                throw new YomsException("删除第三方登陆失败");
            }
        }
    }

    /**
     * 增加第三方登陆
     */
    public void addThirdLand(ThirdLandDomain thirdLandDomain) throws YomsException {

        thirdLandDomain.setOpenId(thirdLandDomain.getOpenId());
        List<ThirdLandDomain> thirdLandDomain1 = thirdLandDao.getDomainCollection(thirdLandDomain);
        if (thirdLandDomain1.size() > 0) {
            throw new YomsException("该数据已存在", 1);
        } else {
            if (thirdLandDao.addDomain(thirdLandDomain) != 1) {
                throw new YomsException("增加第三方登陆失败");
            }
        }
    }

    /**
     * 更新第三方登陆
     */
    public void upThirdLand(ThirdLandDomain thirdLandDomain) throws YomsException {
        ThirdLandDomain domain = thirdLandDao.getDomainById(thirdLandDomain.getId());
        if (domain == null) {
            throw new YomsException("非法请求");
        }
        if (thirdLandDao.updateDomain(domain) != 1) {
            throw new YomsException("更新第三方登陆失败");
        }
    }

}
